include("core/utility/plugin_register.lua");

local function LoadRaptorPlugins()
	local plugin_names = {
		sh = {},
		sv = {},
		cl = {}
	};

	for k, v in pairs( file.Find("lua/plugins/*.lua", "GAME") ) do
		local prefix = string.Left(v, 2);
		
		v = "plugins/"..v;

		if( prefix == "sh" ) then
			if( SERVER ) then
				AddCSLuaFile( v );
				include( v );
			else
				include( v );
			end
		elseif( prefix == "sv" ) then
			if( SERVER ) then
				include( v );
			end
		else
			if( SERVER ) then
				AddCSLuaFile( v );
			else
				include( v );
			end
		end

		table.insert(plugin_names[ prefix ], v);

		local pname = v:match("_([^_]+).lua$");

		local plugin = GetRaptorPlugin( pname );
		if( plugin.Main ) then
			plugin:Main( plugin );
		end

		if( plugin.HandleChatCommand ) then
			RegisterRaptorChatCommand(plugin.command, pname);
		end
	end

	return plugin_names;
end

local ret = LoadRaptorPlugins();
for state, modules in pairs( ret ) do
	if( state == "sh" ) then
		for _, name in pairs( modules ) do
			MsgC(Color(0, 255, 0), "Raptor successfully loaded shared plugin "..name.."\n");
		end
	elseif( state == "sv" ) then
		for _, name in pairs( modules ) do
			MsgC(Color(0, 255, 0), "Raptor successfully loaded server plugin "..name.."\n");
		end
	elseif( state == "cl" ) then
		for _, name in pairs( modules ) do
			MsgC(Color(0, 255, 0), "Raptor successfully loaded client plugin "..name.."\n");
		end
	end
end